<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    node.js+express+mongodb来开发后端 |  Shimmer
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/myBlog/images/ayer.svg" />
  
  
<link rel="stylesheet" href="/myBlog/dist/main.css">

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">
  
<link rel="stylesheet" href="/myBlog/css/custom.css">

  
  <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
  
  

  

<link rel="alternate" href="/myBlog/atom.xml" title="Shimmer" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    
      
      <canvas width="1777" height="841"
        style="position: fixed; left: 0px; top: 0px; z-index: 99999; pointer-events: none;"></canvas>
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-2021-03-10-node.js+express+mongodb来开发后端"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  node.js+express+mongodb来开发后端
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/myBlog/2021/03/11/2021-03-10-node.js+express+mongodb%E6%9D%A5%E5%BC%80%E5%8F%91%E5%90%8E%E7%AB%AF/" class="article-date">
  <time datetime="2021-03-10T16:00:00.000Z" itemprop="datePublished">2021-03-11</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.6k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">7 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id="node-js-express-mongodb来开发后端"><a href="#node-js-express-mongodb来开发后端" class="headerlink" title="node.js+express+mongodb来开发后端"></a>node.js+express+mongodb来开发后端</h3><p>大四临近毕业，要开始写毕业设计，但是作为前端的我，对后端一窍不通。然后想到nodejs也可以写后端。用的就是javascript。应该学习成本不是很高。</p>
<p>如今已经写好了毕设。但是对当时怎么用nodejs+express+mongodb写接口有点遗忘了。</p>
<p>今天来记录一下。以便后面来复习和查找。</p>
<h5 id="1、安装express"><a href="#1、安装express" class="headerlink" title="1、安装express"></a>1、安装express</h5><p><code>npm install express --save</code></p>
<h5 id="2、新建一个js文件"><a href="#2、新建一个js文件" class="headerlink" title="2、新建一个js文件"></a>2、新建一个js文件</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//引入express并绑定到app上</span></span><br><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> app = express()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 静态资源路径</span></span><br><span class="line">app.use(<span class="string">&#x27;/public&#x27;</span>, express.static(<span class="string">&#x27;public&#x27;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment">// 跨域</span></span><br><span class="line">app.use(<span class="built_in">require</span>(<span class="string">&#x27;cors&#x27;</span>)())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// express 处理json</span></span><br><span class="line">app.use(express.json())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 接口的开发</span></span><br><span class="line">app.use(<span class="string">&#x27;/&#x27;</span>,<span class="built_in">require</span>(<span class="string">&#x27;./router/user&#x27;</span>))</span><br><span class="line">app.use(<span class="string">&#x27;/&#x27;</span>,<span class="built_in">require</span>(<span class="string">&#x27;./router/address&#x27;</span>))</span><br><span class="line">app.use(<span class="string">&#x27;/&#x27;</span>,<span class="built_in">require</span>(<span class="string">&#x27;./router/admin&#x27;</span>))</span><br><span class="line">app.use(<span class="string">&#x27;/&#x27;</span>,<span class="built_in">require</span>(<span class="string">&#x27;./router/banner&#x27;</span>))</span><br><span class="line">app.use(<span class="string">&#x27;/&#x27;</span>,<span class="built_in">require</span>(<span class="string">&#x27;./router/cat&#x27;</span>))</span><br><span class="line">app.use(<span class="string">&#x27;/&#x27;</span>,<span class="built_in">require</span>(<span class="string">&#x27;./router/goods&#x27;</span>))</span><br><span class="line"><span class="comment">// app.use(&#x27;/&#x27;,require(&#x27;./router/hotGoods&#x27;))</span></span><br><span class="line">app.use(<span class="string">&#x27;/&#x27;</span>,<span class="built_in">require</span>(<span class="string">&#x27;./router/order&#x27;</span>))</span><br><span class="line">app.use(<span class="string">&#x27;/&#x27;</span>,<span class="built_in">require</span>(<span class="string">&#x27;./router/shopcart&#x27;</span>))</span><br><span class="line">app.use(<span class="string">&#x27;/&#x27;</span>,<span class="built_in">require</span>(<span class="string">&#x27;./router/star&#x27;</span>))</span><br><span class="line">app.use(<span class="string">&#x27;/&#x27;</span>,<span class="built_in">require</span>(<span class="string">&#x27;./router/upload&#x27;</span>))</span><br><span class="line">app.use(<span class="string">&#x27;/&#x27;</span>,<span class="built_in">require</span>(<span class="string">&#x27;./router/goodsBuyCount&#x27;</span>))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 运行端口，服务跑在本地的3002端口上</span></span><br><span class="line">app.listen(<span class="number">3002</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;success in port 3002，服务已经跑起来啦&#x27;</span>);</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h5 id="3、mongoose的使用"><a href="#3、mongoose的使用" class="headerlink" title="3、mongoose的使用"></a>3、mongoose的使用</h5><p>安装mongoose</p>
<p><code>npm i mongoose --save</code></p>
<p>新建一个<code>js</code>文件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//引入mongoose</span></span><br><span class="line"><span class="keyword">const</span> mongoose = <span class="built_in">require</span>(<span class="string">&#x27;mongoose&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 连接数据库</span></span><br><span class="line">mongoose.connect(<span class="string">&#x27;mongodb://localhost:27017/storedb&#x27;</span>, &#123;</span><br><span class="line">    useNewUrlParser: <span class="literal">true</span>,</span><br><span class="line">    useCreateIndex: <span class="literal">true</span>,</span><br><span class="line">    useUnifiedTopology: <span class="literal">true</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">//创建数据模型，类似于表</span></span><br><span class="line"><span class="keyword">const</span> CateSchema = <span class="keyword">new</span> mongoose.Schema(&#123;</span><br><span class="line">    <span class="comment">//字段名称与字段类型 还可以加一些判断。required:true代表必传，unique 代表唯一的</span></span><br><span class="line">    cat_id: &#123; <span class="attr">type</span>:<span class="built_in">Number</span> &#125;,</span><br><span class="line">    cat_name: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//绑定到模型</span></span><br><span class="line"><span class="keyword">const</span> Category = mongoose.model(<span class="string">&#x27;category&#x27;</span>, CateSchema, <span class="string">&#x27;category&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h5 id="4、接口的实现"><a href="#4、接口的实现" class="headerlink" title="4、接口的实现"></a>4、接口的实现</h5><p>这里直接使用express router  不直接用<code>app.get()或者app.post()</code>这样去使用了。</p>
<p>新建一个<code>js</code>文件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//引入express</span></span><br><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>);</span><br><span class="line"><span class="comment">//引入mongoose数据模型</span></span><br><span class="line"><span class="keyword">const</span> &#123; Address &#125; = <span class="built_in">require</span>(<span class="string">&#x27;../storedb&#x27;</span>)</span><br><span class="line"><span class="comment">//绑定router</span></span><br><span class="line"><span class="keyword">let</span> router = express.Router();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取用户地址</span></span><br><span class="line">router.get(<span class="string">&#x27;/getAddress&#x27;</span>, <span class="keyword">async</span> (req, res) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> address = <span class="keyword">await</span> Address.find(&#123; <span class="attr">user_id</span>: req.query.user_id &#125;)</span><br><span class="line">    res.send(address)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 删除地址</span></span><br><span class="line">router.delete(<span class="string">&#x27;/deleteAddress&#x27;</span>, <span class="keyword">async</span> (req, res) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> address = req.query</span><br><span class="line">    <span class="keyword">const</span> userId = req.query.user_id</span><br><span class="line"></span><br><span class="line">    <span class="keyword">await</span> Address.deleteOne(&#123; <span class="attr">user_id</span>: userId, <span class="attr">add_id</span>: address.add_id &#125;)</span><br><span class="line"></span><br><span class="line">    res.send(&#123;</span><br><span class="line">        message: <span class="string">&quot;删除地址成功！&quot;</span></span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 修改地址</span></span><br><span class="line">router.post(<span class="string">&#x27;/changeAddress&#x27;</span>, <span class="keyword">async</span> (req, res) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> address = req.body</span><br><span class="line">    <span class="keyword">const</span> addId = req.body.add_id</span><br><span class="line"></span><br><span class="line">    <span class="keyword">await</span> Address.updateOne(&#123; <span class="attr">add_id</span>: addId &#125;, &#123;</span><br><span class="line">        user_name: address.user_name,</span><br><span class="line">        user_phone: address.user_phone,</span><br><span class="line">        user_address: address.user_address</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    res.send(&#123;</span><br><span class="line">        message: <span class="string">&quot;修改地址成功！&quot;</span></span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">//导出</span></span><br><span class="line"><span class="built_in">module</span>.exports = router</span><br></pre></td></tr></table></figure>
<p>一般查询用get</p>
<p>增加修改用post</p>
<p>删除用delete</p>
<p><strong>说一下mongoose的增删改查</strong></p>
<p><strong>增加</strong></p>
<p>Address代表表名</p>
<p>里面是增加的一个地址，</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Address.create(&#123; <span class="attr">user_id</span>: <span class="number">1234</span>,</span><br><span class="line">        add_id: <span class="number">123</span>,</span><br><span class="line">        user_name: <span class="string">&quot;zz&quot;</span>,</span><br><span class="line">        user_phone: <span class="number">123456789</span>,</span><br><span class="line">        user_address: <span class="string">&quot;大道&quot;</span></span><br><span class="line">               &#125;)</span><br></pre></td></tr></table></figure>
<p><strong>删除</strong></p>
<p>删除user_id中add_id为123的那个地址</p>
<p><code>Address.deleteOne(&#123; user_id: 1234, add_id: 123 &#125;)</code></p>
<p><strong>修改</strong></p>
<p>修改第一个参数是修改哪一个，查找条件。add_id为多少的哪一个。</p>
<p>第二个参数为修改后的数据</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Address<span class="selector-class">.updateOne</span>(&#123; add_id: addId &#125;, &#123;</span><br><span class="line">        user_name: <span class="selector-tag">address</span><span class="selector-class">.user_name</span>,</span><br><span class="line">        user_phone: <span class="selector-tag">address</span><span class="selector-class">.user_phone</span>,</span><br><span class="line">        user_address: <span class="selector-tag">address</span>.user_address</span><br><span class="line">    &#125;)</span><br></pre></td></tr></table></figure>
<p><strong>查询</strong></p>
<figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//查询所有的</span></span><br><span class="line"><span class="module-access"><span class="module"><span class="identifier">Address</span>.</span></span>find<span class="literal">()</span></span><br><span class="line"><span class="comment">//查询user_id为123的所有地址</span></span><br><span class="line"><span class="module-access"><span class="module"><span class="identifier">Address</span>.</span></span>find(&#123;user_id:<span class="number">123</span>&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<p><strong>mongoose的一些常用查询参数</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//skip查询的时候跳过多少个</span></span><br><span class="line"><span class="comment">//limit限制一次查询几个</span></span><br><span class="line"><span class="comment">//sort是排序 1是正序-1是倒序</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// skip(), limit(), sort()三个放在一起执行的时候，执行的顺序是先 sort(), 然后是 skip()，最后是显示的 limit()。</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">const</span> goods = <span class="keyword">await</span> Goods.find().limit(pageSize).skip((pageNum - <span class="number">1</span>) * pageSize).sort(&#123; <span class="attr">goods_id</span>: <span class="number">1</span> &#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//$in 查询goods_id为$in后面的数组</span></span><br><span class="line"><span class="keyword">const</span> aa = <span class="keyword">await</span> Goods.find(&#123; <span class="attr">goods_id</span>: &#123; <span class="attr">$in</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>] &#125; &#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//$or  满足其中一个条件就可以</span></span><br><span class="line"><span class="comment">//$regex 匹配查询</span></span><br><span class="line"><span class="keyword">const</span> data = <span class="keyword">await</span> Goods.find(&#123;</span><br><span class="line">        $or: [</span><br><span class="line">            &#123; <span class="attr">goods_name</span>: &#123; <span class="attr">$regex</span>: reg &#125; &#125;,</span><br><span class="line">            &#123; <span class="attr">goods_title</span>: &#123; <span class="attr">$regex</span>: reg &#125; &#125;,</span><br><span class="line">            &#123; <span class="attr">goods_subtitle</span>: &#123; <span class="attr">$regex</span>: reg &#125; &#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//aggregate聚合查询</span></span><br><span class="line"><span class="comment">//$lookup  两个表关联查询</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//下面的意思是根据cat_id在goods表中查询，查询到的数据放到goods这个字段下。</span></span><br><span class="line"><span class="keyword">const</span> index = req.params.categoryId - <span class="number">1</span></span><br><span class="line">    <span class="keyword">await</span> Category.aggregate([</span><br><span class="line">        &#123;</span><br><span class="line">            $lookup: &#123;</span><br><span class="line">                <span class="keyword">from</span>: <span class="string">&quot;goods&quot;</span>,</span><br><span class="line">                localField: <span class="string">&quot;cat_id&quot;</span>,</span><br><span class="line">                foreignField: <span class="string">&quot;cat_id&quot;</span>,</span><br><span class="line">                <span class="keyword">as</span>: <span class="string">&quot;goods&quot;</span>,</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,&#123;<span class="attr">$sort</span>:&#123;<span class="string">&quot;cat_id&quot;</span>:<span class="number">1</span>&#125;&#125;</span><br><span class="line">    ]</span><br><span class="line">        , <span class="function">(<span class="params">err, data</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (err) &#123;</span><br><span class="line">                <span class="built_in">console</span>.log(err);</span><br><span class="line">                <span class="keyword">return</span></span><br><span class="line">            &#125;</span><br><span class="line">            res.send(data[index])</span><br><span class="line">        &#125;</span><br><span class="line">    )</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h5 id="5、上传文件"><a href="#5、上传文件" class="headerlink" title="5、上传文件"></a>5、上传文件</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> formidable = <span class="built_in">require</span>(<span class="string">&#x27;formidable&#x27;</span>);</span><br><span class="line">fs = <span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> &#123; Goods &#125; = <span class="built_in">require</span>(<span class="string">&#x27;../storedb&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> router = express.Router();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">router.post(<span class="string">&#x27;/uploadImg&#x27;</span>, <span class="function">(<span class="params">req, res, next</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> goods = req.query</span><br><span class="line">    <span class="keyword">let</span> form = <span class="keyword">new</span> formidable.IncomingForm();</span><br><span class="line">    form.encoding = <span class="string">&#x27;utf-8&#x27;</span>; <span class="comment">// 编码</span></span><br><span class="line">    <span class="comment">// 保留扩展名</span></span><br><span class="line">    form.keepExtensions = <span class="literal">true</span>;</span><br><span class="line">    <span class="comment">//文件存储路径 最后要注意加 &#x27;/&#x27; 否则会被存在public下</span></span><br><span class="line">    form.uploadDir = path.join(__dirname, <span class="string">&#x27;../public/imgs/accessory/&#x27;</span>)</span><br><span class="line">    form.parse(req,  <span class="function">(<span class="params">err, fields, files</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (err) <span class="keyword">throw</span> err</span><br><span class="line">        <span class="keyword">let</span> oldFilename = files.file.name</span><br><span class="line">        fs.rename(files.file.path, form.uploadDir + generateFilename(oldFilename), <span class="keyword">async</span> err =&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (err) &#123;</span><br><span class="line">                <span class="built_in">console</span>.log(<span class="string">&quot;重命名失败&quot;</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">              <span class="keyword">await</span> Goods.updateOne(&#123; <span class="attr">goods_id</span>: goods.goods_id &#125;,</span><br><span class="line">                    &#123;</span><br><span class="line">                        goods_pic: <span class="string">`public/imgs/accessory/<span class="subst">$&#123;generateFilename(oldFilename)&#125;</span>`</span>,</span><br><span class="line">                    &#125;)</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;)</span><br><span class="line">                 res.send(&#123;</span><br><span class="line">                     message: <span class="string">&quot;保存成功！&quot;</span></span><br><span class="line">                 &#125;)   </span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">generateFilename</span>(<span class="params">oldFilename</span>) </span>&#123;</span><br><span class="line">        <span class="comment">//将老的文件名拼上时间戳，这样既不会命名冲突又可以看出文件的上传事件</span></span><br><span class="line">        <span class="keyword">let</span> d = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line">        <span class="keyword">let</span> names = oldFilename.split(<span class="string">&quot;.&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">`<span class="subst">$&#123;names[<span class="number">0</span>]&#125;</span>_<span class="subst">$&#123;<span class="string">&quot;&quot;</span> + d.getFullYear() + (d.getMonth() + <span class="number">1</span>) + d.getDate() + <span class="string">&#x27;_&#x27;</span> + d.getHours() + d.getMinutes() + d.getSeconds()&#125;</span>.<span class="subst">$&#123;names[<span class="number">1</span>]&#125;</span>`</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h5 id="6、启动"><a href="#6、启动" class="headerlink" title="6、启动"></a>6、启动</h5><p>在项目根目录打开终端运行<code>node app.js</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//出现   success in port 3002，服务已经跑起来啦 </span></span><br><span class="line"></span><br><span class="line"><span class="comment">//因为上面是这样写的</span></span><br><span class="line"><span class="comment">// 运行端口，服务跑在本地的3002端口上</span></span><br><span class="line">app.listen(<span class="number">3002</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;success in port 3002，服务已经跑起来啦&#x27;</span>);</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>说明运行成功。</p>
<h5 id="7、调用接口"><a href="#7、调用接口" class="headerlink" title="7、调用接口"></a>7、调用接口</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">// 获取用户地址</span></span><br><span class="line">    <span class="keyword">async</span> getAddress () &#123;</span><br><span class="line">      <span class="keyword">const</span> userId = <span class="built_in">window</span>.sessionStorage.getItem(<span class="string">&#x27;userId&#x27;</span>)</span><br><span class="line">      <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="built_in">this</span>.$http.get(<span class="string">&#x27;getAddress&#x27;</span>, &#123;</span><br><span class="line">        params: &#123;</span><br><span class="line">          user_id: userId</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="built_in">this</span>.address = res.data</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//请求地址</span></span><br><span class="line">Request URL: http:<span class="comment">//localhost:3002/getAddress?user_id=1255</span></span><br><span class="line">Request Method: GET</span><br><span class="line">Status Code: <span class="number">200</span> OK</span><br><span class="line">Remote Address: [::<span class="number">1</span>]:<span class="number">3002</span></span><br><span class="line">Referrer Policy: strict-origin-when-cross-origin</span><br><span class="line"><span class="comment">//返回相应的数据</span></span><br><span class="line">[&#123;</span><br><span class="line">  <span class="string">&quot;_id&quot;</span>:<span class="string">&quot;602e26a4a8185949dc9ea23e&quot;</span>,</span><br><span class="line">    <span class="string">&quot;user_id&quot;</span>:<span class="string">&quot;1255&quot;</span>,</span><br><span class="line">    <span class="string">&quot;add_id&quot;</span>:<span class="string">&quot;157&quot;</span>,</span><br><span class="line">    <span class="string">&quot;user_name&quot;</span>:<span class="string">&quot;wky&quot;</span>,</span><br><span class="line">    <span class="string">&quot;user_phone&quot;</span>:<span class="string">&quot;18888888888&quot;</span>,</span><br><span class="line">    <span class="string">&quot;user_address&quot;</span>:<span class="string">&quot;无锡市滨湖区太湖学院北区&quot;</span>,</span><br><span class="line">    <span class="string">&quot;__v&quot;</span>:<span class="number">0</span></span><br><span class="line">&#125;]</span><br></pre></td></tr></table></figure>


<p>到这里基本上就完成了大部分的功能。</p>
<p>当然还有登录注册。jwt签名生成token，密码的加密等等。</p>
<p>这个我也忘记的差不多了。</p>
<p>去整理一下，再记录下来。</p>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://gitee.com/shimmer612/myBlog/2021/03/11/2021-03-10-node.js+express+mongodb%E6%9D%A5%E5%BC%80%E5%8F%91%E5%90%8E%E7%AB%AF/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/myBlog/tags/express/" rel="tag">express</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/myBlog/tags/mongodb/" rel="tag">mongodb</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/myBlog/tags/nodejs/" rel="tag">nodejs</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/myBlog/2021/03/12/2021-03-12-CSS%E7%9A%84%E4%B8%80%E4%BA%9B%E7%9F%A5%E8%AF%86%E7%82%B9/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            CSS的一些知识点
          
        </div>
      </a>
    
    
      <a href="/myBlog/2021/03/10/2021-03-10-%E4%BB%8E%E6%95%B0%E7%BB%84%E4%B8%AD%E5%8F%96%E5%87%BA%E9%9A%8F%E6%9C%BA%E5%87%A0%E4%B8%AA%E6%95%B0%E7%BB%84%E6%88%90%E6%96%B0%E6%95%B0%E7%BB%84%EF%BC%88%E4%B8%8D%E9%87%8D%E5%A4%8D%E5%8F%96%EF%BC%89/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">从数组中取出随机几个数组成新数组（不重复取）</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "xGZKFxfEhEyqsK7YA4NNoQNr-gzGzoHsz",
    app_key: "qCe4kOh0W9rCgNWcv31cO3NF",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "说出你想说的吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
     
</article>

</section>
      <!--
 * @Author: zhangzhen
 * @Date: 2023-02-04 10:30:09
 * @LastEditors: zhangzhen
 * @LastEditTime: 2023-05-23 09:47:12
 * 
-->
<footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2021-2023
        <i class="ri-heart-fill heart_icon"></i> Shimmer
      </li>
    </ul>
    <!-- <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul> -->
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/myBlog/"><img src="/myBlog/images/ayer-side.svg" alt="Shimmer"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/archives">目录</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/photos">相册</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/2021/02/12/2021-02-12-%E5%85%B3%E4%BA%8E%E6%88%91/">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/message">留言板</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/myBlog/js/jquery-2.0.3.min.js"></script>


<script src="/myBlog/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/myBlog/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/myBlog/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/myBlog/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->


<script src="/myBlog/js/clickBoom2.js"></script>


<!-- CodeCopy -->


<link rel="stylesheet" href="/myBlog/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>